package com.skivingcloud.workflow.service.impl;

import com.skivingcloud.common.exception.BusinessException;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.StringUtils;
import com.skivingcloud.common.utils.UuidUtil;
import com.skivingcloud.workflow.entity.Delegation;
import com.skivingcloud.workflow.mapper.DelegationMapper;
import com.skivingcloud.workflow.service.IDelegationService;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.joda.time.LocalDateTime;
import org.springframework.stereotype.Service;

import net.sf.json.JSONObject;

/**
 * <p>
 * 转授权表 服务实现类
 * </p>
 *
 * @author skivingcloud
 * @since 2022-03-03
 */
@Service
public class DelegationServiceImpl extends ServiceImpl<DelegationMapper, Delegation> implements IDelegationService {

	// 添加授权记录
	@Override
	public void addDelegation(Delegation delegation) {
		// try {
		delegation.setId(UuidUtil.get32UUID());
		this.baseMapper.insert(delegation);
	}

	@Override
	public void updateDelegation(Delegation delegation) {
		this.baseMapper.updateById(delegation);
	}

	// 查询授权信息
	@Override
	public Delegation findById(String id) {
		Delegation delegation = this.baseMapper.selectById(id);
		return delegation;
	}

	// 查询分页列表
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		try {

			PageUtils pageUtils = PageUtils.getPageInfo(params);
			Page<Object> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
			Delegation delegation = (Delegation) JSONObject.toBean(JSONObject.fromObject(params),
					Delegation.class);
			if (delegation != null && StringUtils.isNotBlank(delegation.getAuthId())) {
				delegation.setAuthId("%" + delegation.getAuthId().trim() + "%");
			} else {
				delegation.setAuthId(null);
			}
			if (delegation != null && StringUtils.isNotBlank(delegation.getBeAuthId())) {
				delegation.setBeAuthId("%" + delegation.getBeAuthId().trim() + "%");
			} else {
				delegation.setBeAuthId(null);
			}
			List<Map<String, Object>> mapList = this.baseMapper.selectPageByPar(delegation);
			// Integer count = delegationDao.count(delegation);
			// pageUtils.setList(mapList);
			// pageUtils.setTotalCount(count);
			pageUtils = new PageUtils(p);
			return pageUtils;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 删除授权记录
	@Override
	public void deleteDelegation(String[] ids) {
		if (ids == null) {
			// return R.error("没有选择要删除的记录。");
			throw new BusinessException("没有选择要删除的记录");
		}
		this.baseMapper.deleteBatchIds(Arrays.asList(ids));
		// // try {
		// for (String id : ids) {
		// 	delegationDao.deleteDelegation(id);
		// }

	}

	@Override
	public List<Delegation> getDelegationByUserId(String userId) {
		// 将查询时间处理为00:00:00
		LocalDateTime date = LocalDateTime.now();
		date.plusHours(0);
		date.plusMinutes(0);
		date.plusSeconds(0);
		date.plusMillis(0);
		QueryWrapper<Delegation> queryWrapper = new QueryWrapper<Delegation>();
		queryWrapper.eq("auth_id", userId);
		queryWrapper.le("start_time", date);
		queryWrapper.ge("end_time", date);
		return this.baseMapper.selectList(queryWrapper);
	}

	/**
	 * 根据用户id和时间查询授权记录
	 * 
	 * @param pp
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getDelegationByUserIdAndTime(Map<String, Object> pp) {
		return this.baseMapper.getDelegationByUserIdAndTime(pp);
	}

}
